<?php

namespace app\forms\menu;

use yii\db\Exception;
use app\forms\Model;
use app\models\Menu;
use yii\web\HttpException;

/**
 * 菜单 搜索推荐
 */
class SuggestForm extends Model
{
    public $q;

    public function rules()
    {
        return [
            ['q', 'trim'],
            ['q', 'string', 'max' => 32],
        ];
    }

    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function suggest($param)
    {
        $form = new self();
        $form->setAttributes($param);
        if ($form->validate()) {
            try {
                return (new Menu)->getSuggest($form->q);
            } catch (Exception $e) {
                $form->serverError();
            }
        }
    }
}